﻿using Bogus;
using IceZero.Business;
using IceZero.Business.Business;
using IceZero.DataStore;
using IceZero.DataStore.Models;
using IceZero.Web.Helpers;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IceZero.Web.Controllers
{
    /// <summary>
    /// 
    /// </summary>
    [ApiExplorerSettings(IgnoreApi = false,GroupName ="version_test")]
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class CatController : ControllerBase
    {
        private readonly CatBusiness _catBusiness;
        private readonly UserBusiness _userBusiness;
        public CatController(CatBusiness catBusiness, UserBusiness userBusiness)
        {
            _catBusiness = catBusiness;
            _userBusiness = userBusiness;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetAllCatCount()
        {
            return ActionHelper.ExecAction(this, controller =>
            {
                return _catBusiness.CatDataStore.Count();
            });
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="cat"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddCat(Cat cat)
        {
            return ActionHelper.ExecAction(this, controller =>
            {
                return _catBusiness.CatDataStore.Save<string>(cat);
            });
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetById(string id)
        {
            return ActionHelper.ExecAction(this, controller =>
            {
                return _catBusiness.CatDataStore.QueryById(id);
            });
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="count"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult AddMockCat(int count = 10)
        {
            return ActionHelper.ExecAction(this, controller =>
            {
                var stopwatch = Stopwatch.StartNew();
                var root = _userBusiness.UserDataStore.GetRoot();
                for (int i = 0; i < count; i++)
                {
                    var cat = new Faker<Cat>()
                    .RuleFor(o => o.Name, f => f.Name.FirstName())
                    .RuleFor(o => o.Weight, f => f.Random.Float(2.1F, 10.5F))
                    .RuleFor(o => o.Id, f => Guid.NewGuid().ToString("N"))
                    .RuleFor(o => o.Sex, f => f.Random.Int(1000) % 2 == 0 ? "male" : "female")
                    .RuleFor(o => o.Code, f => f.Random.Int(1, 10000))
                    .Generate();
                    cat.CreatorId = root.Id;
                    cat.UpdaterId = root.Id;
                    _catBusiness.CatDataStore.Save(cat);
                }
                stopwatch.Stop();
                Console.WriteLine($"Cost Time(ms):{stopwatch.ElapsedMilliseconds}");
            });
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="count"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult AddMockCat2(int count = 10)
        {
            return ActionHelper.ExecAction(this, controller =>
            {
                var stopwatch = Stopwatch.StartNew();
                var root = _userBusiness.UserDataStore.GetRoot();
                for (int i = 0; i < count; i++)
                {
                    NHibernateHelper.UseSession(session =>
                    {
                        var cat = new Faker<Cat>()
                        .RuleFor(o => o.Name, f => f.Name.FirstName())
                        .RuleFor(o => o.Weight, f => f.Random.Float(2.1F, 10.5F))
                        .RuleFor(o => o.Id, f => Guid.NewGuid().ToString("N"))
                        .RuleFor(o => o.Sex, f => f.Random.Int(1000) % 2 == 0 ? "male" : "female")
                        .RuleFor(o => o.Code, f => f.Random.Int(1, 10000))
                        .Generate();
                        cat.CreatorId = root.Id;
                        cat.UpdaterId = root.Id;
                        session.Save(cat);
                    });
                }
                stopwatch.Stop();
                Console.WriteLine($"Cost Time(ms):{stopwatch.ElapsedMilliseconds}");
            });
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="count"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult AddMockCat3(int count = 10)
        {
            return ActionHelper.ExecAction(this, controller =>
            {
                var stopwatch = Stopwatch.StartNew();
                var root = _userBusiness.UserDataStore.GetRoot();
                for (int i = 0; i < count; i++)
                {
                    using(var session = NHibernateHelper.GetNewSession())
                    {
                        var cat = new Faker<Cat>()
                        .RuleFor(o => o.Name, f => f.Name.FirstName())
                        .RuleFor(o => o.Weight, f => f.Random.Float(2.1F, 10.5F))
                        .RuleFor(o => o.Id, f => Guid.NewGuid().ToString("N"))
                        .RuleFor(o => o.Sex, f => f.Random.Int(1000) % 2 == 0 ? "male" : "female")
                        .RuleFor(o => o.Code, f => f.Random.Int(1, 10000))
                        .Generate();
                        cat.CreatorId = root.Id;
                        cat.UpdaterId = root.Id;
                        session.Save(cat);
                        session.Flush();
                    };
                }
                stopwatch.Stop();
                Console.WriteLine($"Cost Time(ms):{stopwatch.ElapsedMilliseconds}");
            });
        }
    }
}
